﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudReader.Net.Server
{
    class DeamonThread
    {
        Task t;
        ConnectionPool _pool;
        bool run = true;
        public DeamonThread(ref ConnectionPool pool)
        {
            _pool = pool;
            t = new Task(new Action(Deamon));
            t.Start();
        }
        async void Deamon()
        {
            while (run)
            {
                for (int i = 0; i < _pool._conn.Count; i++)
                {
#if DEBUG
                    if ((DateTime.Now - _pool[i].ActiveTime).Seconds >= 3)
                    {
                        OnBadConnectionRaised?.Invoke(_pool[i]);
                    }
#else
                    if ((_pool[i].ActiveTime - DateTime.Now).Minutes == 1)
                    {
                        OnBadConnectionRaised?.Invoke(_pool[i]);
                    }
#endif

                }
                await Task.Delay(5000);
            }
        }
        public event Action<Connection> OnBadConnectionRaised;
        public void Stop()
        {
            run = false;
        }
    }
}
